
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tests;

import entities.tools.Form;
import entities.tools.Question;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import util.ejb.FormBeanInterface;

/**
 *
 * @author Marek
 */
@WebServlet(name = "test2Servlet", urlPatterns = {"/test2"})
public class FormBeanTest extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @EJB
    FormBeanInterface formBean;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Test of DB entity model</title>");

            out.println("</head>");
            out.println("<body>");
            out.println("<h1>We are going to test if the form entity is created correctly, and if it is the updated correctly</h1><br/>");
            out.println("Remember. Clean database before testing<br/>");

            Form form = null;
            form = formBean.createNewForm();

            for (int x = 0; x < 200; x++) {
                formBean.createNewForm();
            }

            out.println("Form1 have been created<br/>" + form.getId());
            form.setName("Form1 name");
            form.setDescription("Form1 description");
            formBean.updateForm(form);
            out.println("Form1 have been updated. Now it should have had name set to 'Form1 name' and description set to 'Form1 description'<br/>");

            Form entity1 = new Form();
            entity1.setId(new Long(2000));
            entity1.setName("Test name");
            formBean.updateForm(entity1);

            formBean.deleteForm(form);

            formBean.deleteForm(entity1);

            List<Form> forms = formBean.getAllForms();
            for (Form form1 : forms) {
                long Long = form1.getId().longValue();
                if (Long % 2 == 0) {
                    out.println("<br/>Deleting:" + form1.getId());
                    formBean.deleteForm(form1);
                } else {
                    out.println("<br/>Doing nothing with:" + form1.getId());
                }
            }

            Form existingForm = formBean.getExistingForm(new Long(227));
            existingForm.setName("This form (id:" + existingForm.getId() + ") was get by getExistingForm method, and updated by updateForm method");
            formBean.updateForm(existingForm);
            List<Question> questions = formBean.getAllQuestions(existingForm);
            for(Question question:questions){
                out.println("Deleting question id="+((Question)question).getId()+"<br/>");
                formBean.removeQuestion(existingForm,(Question)question);
            }
            Long last_id =new Long(0);
            for (int x = 0; x < 100; x++) {
                Question question = formBean.createNewQuestion(existingForm);
                question.setQuestion("Question "+x);
                question.setDescription("Question "+x+" was created by the dbmodel test1 servlet");
                formBean.updateQuestion(existingForm, question);
                out.println("Creating a new question id="+question.getId()+"<br/>");
                last_id = question.getId()-1;
            }
            
            Question existingQuestion = formBean.getQuestion(existingForm,last_id);
            out.println("Question id="+existingQuestion.getId()+" has been read from the database");
            existingQuestion.setQuestion("New question for the question id="+existingQuestion.getId());
            existingQuestion.setDescription("New description for the question id="+existingQuestion.getId());
            formBean.updateQuestion(existingForm,existingQuestion);
            out.println("This questiong have been modified and persisted to database");
            
            /*try {
             formBean.deleteForm(form);
             } catch (Exception ex) {
             out.println(ex.getMessage());
             out.println(ex.getStackTrace().toString());
             }
             out.println("Form 1 have been deleted. form local variable should be set to null'<br/>");
             if (form != null) {
             out.println("Error: local variable is not null, as it is expected here!");
             }
             try {
             form = formBean.createNewForm();
             } catch (Exception ex) {
             out.println(ex.getMessage());
             out.println(ex.getStackTrace().toString());
             }
             form.setName("Form2");
             form.setDescription("Form2 description");
             try {
             formBean.updateForm(form);
             } catch (Exception ex) {
             out.println(ex.getMessage());
             out.println(ex.getStackTrace().toString());
             }
             out.println("Form2 has been created. Its name has been set to 'Form2'. Its descritpion has been set to 'Form2 description'");
   

             Question question1 = null;
             Question question2 = null;
             Question question3 = null;
             Question question4 = null;
             Question question5 = null;
             try {
             question1 = formBean.createNewQuestion(form);
             question2 = formBean.createNewQuestion(form);
             question3 = formBean.createNewQuestion(form);
             question4 = formBean.createNewQuestion(form);
             question5 = formBean.createNewQuestion(form);
             } catch (Exception ex) {
             out.println(ex.getMessage());
             out.println(ex.getStackTrace().toString());
             }
             question1.setQuestion("Question 1 question");
             question2.setQuestion("Question 2 question");
             question3.setQuestion("Question 3 question");
             question4.setQuestion("Question 4 question");
             question5.setQuestion("Question 5 question");

             out.println("IDIDID:"+question1.getId());
             try {
             formBean.updateQuestion(form, question1);
             formBean.updateForm(form);
             } catch (Exception ex) {
             out.println(ex.getMessage());
             out.println(ex.getStackTrace().toString());
             }

             out.println("</body>");
             out.println("</html>");*/
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
